//
//  ModifyPwdVC.swift
//  whale
//
//  Created by topnet on 2022/7/27.
//

import UIKit

final class ModifyPwdVC: ICBaseVC {

    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func _initData() {
        accountLbl.text = "当前账号"
    }
    
    override func _initUI() {
        super._initUI()
        navBar.titleV.text = "修改密码"
        
        view.da.addSubviews(sub: [
            accountLbl,
            pwdTf1,
            pwdTf2,
            pwdTf3,
            nextBtn
        ])
    }
    
    override func _initFrame() {
        super._initFrame()
        
        accountLbl.left_right(view, x: 10.adapter).height(30.adapter).top(navBar.frame.maxY + 30.adapter)
        pwdTf1.left_right_offsetTop(accountLbl, x: 10.adapter).height(50.adapter)
        pwdTf2.left_right_height_offsetTop(pwdTf1, x: 10.adapter)
        pwdTf3.left_right_height_offsetTop(pwdTf2, x: 10.adapter)
        nextBtn.left_right_height_offsetTop(pwdTf3, x: 20.adapter)
        nextBtn.setBackgroundImage(UIImage.createImg(color: ._blue, rect: nextBtn.bounds), for: .normal)
        nextBtn.setBackgroundImage(UIImage.createImg(color: .bg, rect: nextBtn.bounds), for: .disabled)
    }
    
    private func actionNext() {
        
    }
    
    private func textChange(_ str: String) {
        if (pwdTf1.text?.count ?? 0) > 0 && (pwdTf2.text?.count ?? 0) > 0 && (pwdTf3.text?.count ?? 0) > 0 {
            nextBtn.isEnabled = true
        }else {
            nextBtn.isEnabled = false
        }
    }
    
    private lazy var accountLbl: UILabel = {
        let lbl = UILabel()
        lbl.textColor = .title
        lbl.font = .font(17)
        return lbl
    }()
    
    private lazy var pwdTf1: LoginTextField = {
       let tf = LoginTextField(isPwd: true)
        tf.font = .font(15)
        tf.hintText = "密码"
        tf.leftView = {
            let lbl = UILabel()
            lbl.textColor = .subTitle
            lbl.font = .font(15)
            lbl.text = "旧密码"
            return lbl
        }()
        tf.leftViewWidth = 75.adapter
        tf.leftViewMode = .always
        tf.rx.text.orEmpty.changed.subscribe { [weak self] str in
            self?.textChange(str)
        }.disposed(by: disposeBag)
        return tf
    }()
    
    private lazy var pwdTf2: LoginTextField = {
       let tf = LoginTextField(isPwd: true)
        tf.font = .font(15)
        tf.hintText = "密码"
        tf.leftView = {
            let lbl = UILabel()
            lbl.textColor = .subTitle
            lbl.font = .font(15)
            lbl.text = "新密码"
            return lbl
        }()
        tf.leftViewWidth = 75.adapter
        tf.leftViewMode = .always
        tf.rx.text.orEmpty.changed.subscribe { [weak self] str in
            self?.textChange(str)
        }.disposed(by: disposeBag)
        return tf
    }()
    
    private lazy var pwdTf3: LoginTextField = {
       let tf = LoginTextField(isPwd: true)
        tf.font = .font(15)
        tf.hintText = "密码"
        tf.leftView = {
            let lbl = UILabel()
            lbl.textColor = .subTitle
            lbl.font = .font(15)
            lbl.text = "确认密码"
            return lbl
        }()
        tf.leftViewWidth = 75.adapter
        tf.leftViewMode = .always
        tf.rx.text.orEmpty.changed.subscribe { [weak self] str in
            self?.textChange(str)
        }.disposed(by: disposeBag)
        return tf
    }()
    
    private lazy var nextBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitleColor(.white, for: .normal)
        btn.setTitleColor(.content, for: .disabled)
        btn.setTitle("确定", for: .normal)
        btn.layer.cornerRadius = 25.adapter
        btn.layer.masksToBounds = true
        btn.isEnabled = false
        btn.rx.tap.throttle(.milliseconds(800), scheduler: MainScheduler.instance).subscribe { [weak self] _ in
            self?.actionNext()
        }.disposed(by: disposeBag)
        return btn
    }()

    private lazy var disposeBag = DisposeBag()
}
